<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller {
	//默认为登录页
	public function index() {
		$this->display();
	}
	
	//登录处理页面
	public function loginHandle() {
		if(!IS_POST){
			$this->error('非法请求');
		}else{
			$username = I('username');
			$password = md5(I('password'));
			
			$where = array('username'=>$username);
			$result = M('user')->where($where)->find();
			
			if(!$result || $result['password'] != $password) {
				$this->error('Username or password is wrong.', U('Login/index'));
			}
			
			session('uid', $result['id']);
			session('username', $username);
			
			$this->success('登录成功', U('Index/index'));
		}
	}
	
	//退出
	public function logout() {
		if(isset($_SESSION['uid'])){
			session_unset();
			session_destroy();
			
			$this->success('退出成功', U('Login/index'));
		}
	}
	
}

?>